///:Demo5.java
package com.cqgcxy.stranger.case5;


import com.cqgcxy.stranger.domain.User;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author icechen1219
 * @date 2018/11/20
 */
public class Demo5 {
    /**
     * 序列化（将对象写入文件）
     *
     * @throws Exception
     */
    public static void serilaze() throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.obj"));
        User user1 = User.createUser(11);
        oos.writeObject(user1);
        oos.close();
    }

    /**
     * 反序列化（从文件还原对象）
     *
     * @throws Exception
     */
    public static void deserilaze() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.obj"));
        Object o = ois.readObject();
        System.out.println(o.getClass());
        User user = (User) o;
        System.out.println(user.getName());
    }

    public static void main(String[] args) throws Exception {
        serilaze();
        deserilaze();
    }
}
///:Demo5.java
